#ifndef _uart_H_
#define _uart_H_

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/sem.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <semaphore.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include "timer.h"
#include "global_val.h"

//定时器
#define TimeClose        0
#define TimeOpen         1

//通信协议结构定义
#define uart_0xa5        1 //0xa5
#define uart_0x5a        2 //0x5a
#define uart_length_H    3 //length high 8 bits
#define uart_length_L    4 //length low 8 bits
#define uart_cmd         5 //CMD byte
#define uart_nouseful    6 //No useful byte
#define uart_databytes   7 //Data bytes

#define uart_first_0x5a  8
#define uart_secend_0xa5 9

#define MAX_resend 10 //最大重发次数

#define NOACK    0
#define SuccsACK 1
#define FailACK  2

typedef struct
{
	unsigned short length;//数据包的字节数
	unsigned char  cmd;//命令
	unsigned char  no_use;//备用字节
	unsigned char  data[128];//数据存储地址
	unsigned short crc16;//校验字节
}uart_communication_dmwz;

int usart_setup(const unsigned char *uart_root);
unsigned short CRC16_Uart_send(void);
void uart_send_Bty(const unsigned char* buf, const unsigned short len);
void UART_Recive_Data_Handle(void);
void CmdSendUart(unsigned char cmd_uart, unsigned char* apdubuf, unsigned short apdulength);
void *Pthread_UART_Send(void *arg);
void *Pthread_UART_Receive(void *arg);
void *Pthread_UART_Receive_Data_Handle(void *arg);
void zb_dev_online_handle(uartmsg *msg);
void zb_dev_offline_handle(uartmsg *msg);
void zb_sensor_light_handle(uartmsg *msg);
void zb_sensor_llumination_handle(uartmsg *msg);
void zb_sensor_temp_handle(uartmsg *msg);
void zb_sensor_humidity_handle(uartmsg *msg);
void zb_sensor_door_handle(uartmsg *msg);
void zb_sensor_fan_handle(uartmsg *msg);
void zb_sensor_beep_handle(uartmsg *msg);
void zb_sensor_smoke_handle(uartmsg *msg);
void zb_sensor_calorifier_handle(uartmsg *msg);
void zb_sensor_curtain_handle(uartmsg *msg);

#endif
